package hoang.android.app.ui.missatool;

import hoang.android.app.R;
import hoang.android.app.info.WebServiceConfig;
import hoang.android.app.json.ParameterFactory;
import hoang.android.app.model.Account;
import hoang.android.app.network.AsyncHttpPost;
import hoang.android.app.network.AsyncHttpResponseProcess;
import hoang.android.app.ui.BaseActivity;
import hoang.android.app.utility.StringUtility;

import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.text.format.Time;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

public class VehicleInfoActivity extends BaseActivity {

	Spinner sp_vehicle_class, sp_state_1, sp_state_2;

	EditText et_plate_1, et_plate_2, et_year_1, et_year_2, et_make_1,
			et_make_2, et_model_1, et_model_2, et_color_1, et_color_2;

	String state1, state2;
	int index;
	
	private Account account;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_vehicle_info);

		loadUI();
		listenUI();
		setHeader(true,
				baseActivity.getResources().getString(R.string.vehicle_info));
	}

	@Override
	protected void loadUI() {
		super.loadUI();

		sp_vehicle_class = (Spinner) findViewById(R.id.sp_vehicle_class);
		sp_state_1 = (Spinner) findViewById(R.id.sp_state_1);
		sp_state_2 = (Spinner) findViewById(R.id.sp_state_2);

		et_plate_1 = (EditText) findViewById(R.id.et_place_1);
		et_year_1 = (EditText) findViewById(R.id.et_year_1);
		et_make_1 = (EditText) findViewById(R.id.et_make_1);
		et_model_1 = (EditText) findViewById(R.id.et_model_1);
		et_color_1 = (EditText) findViewById(R.id.et_color_1);

		et_plate_2 = (EditText) findViewById(R.id.et_place_2);
		et_year_2 = (EditText) findViewById(R.id.et_year_2);
		et_make_2 = (EditText) findViewById(R.id.et_make_2);
		et_model_2 = (EditText) findViewById(R.id.et_model_2);
		et_color_2 = (EditText) findViewById(R.id.et_color_2);
		
		//account = new Account();
		account = application.getAccount();
	}

	public void listenUI() {

		findViewById(R.id.img_next).setOnClickListener(this);
		sp_vehicle_class.setAdapter(new ArrayAdapter<String>(baseActivity,
				android.R.layout.simple_spinner_item, getResources()
						.getStringArray(R.array.vehicle_class)));
		sp_vehicle_class
				.setOnItemSelectedListener(new OnItemSelectedListener() {

					@Override
					public void onItemSelected(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {

						index = arg2;
						if (arg2 != 0) {
							findViewById(R.id.ln_trailer_info).setVisibility(
									View.VISIBLE);
						} else {
							findViewById(R.id.ln_trailer_info).setVisibility(
									View.GONE);
						}

					}

					@Override
					public void onNothingSelected(AdapterView<?> arg0) {
						index = 0;
						findViewById(R.id.ln_trailer_info).setVisibility(
								View.GONE);
					}
				});

		sp_state_1.setAdapter(new ArrayAdapter<String>(baseActivity,
				android.R.layout.simple_spinner_item, getResources()
						.getStringArray(R.array.state)));
		sp_state_1.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				state1 = arg0.getItemAtPosition(arg2).toString();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				state1 = "AK";
			}
		});
		sp_state_2.setAdapter(new ArrayAdapter<String>(baseActivity,
				android.R.layout.simple_spinner_item, getResources()
						.getStringArray(R.array.state)));
		sp_state_2.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				state2 = arg0.getItemAtPosition(arg2).toString();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				state2 = "AK";
			}
		});
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.img_next:
			onClickNextButton();

			break;

		default:
			break;
		}
	}

	// -----------------clickbutton next------------------//
	public void onClickNextButton() {
		if (checkValidate()) {

			Account account = application.getAccount();

			account.setPlate1(et_plate_1.getText().toString().trim());
			account.setState1(state1);
			account.setYear1(et_year_1.getText().toString().trim());
			account.setMake1(et_make_1.getText().toString().trim());
			account.setModel1(et_model_1.getText().toString().trim());
			account.setColor1(et_color_1.getText().toString().trim());

			if (index == 1 || index == 2) {
				account.setPlate2(et_plate_2.getText().toString().trim());
				account.setState2(state2);
				account.setYear2(et_year_2.getText().toString().trim());
				account.setMake2(et_make_2.getText().toString().trim());
				account.setModel2(et_model_2.getText().toString().trim());
				account.setColor2(et_color_2.getText().toString().trim());
			}
			
			account.setVehicle_class(getVehicleClass());
			
			loadUserInfo();
		}
	}
	
	//------------get vehicle class-------------------//
	public String getVehicleClass(){
		
		String _vehicle = null;
		
		switch (index) {
		case 0:
			_vehicle = "2";
			break;
		case 1:
			_vehicle = "3to4";
			break;
		case 2:
			_vehicle = "5to6";
			break;

		default:
			break;
		}
		return _vehicle;
	}
	
	//---------------load user info-------------------//
	public void loadUserInfo(){
		//final Account account = application.getAccount();
		
		//account = new Account();
		//account =application.getAccount();
		
		List<NameValuePair> params = null;
		params = ParameterFactory.createParamsSubmitUserInfo(
				account.getAddress_title(), account.getAddress_first_name(), account.getAddress_last_name(), account.getAddress_middle_name(), 
				account.getAddress1(), account.getAddress_city(), account.getAddress_state(), account.getAddress_zipcode(), account.getWorkphone(), 
				account.getAddressEmail(), account.isEmail_receipt()?"true":"false", account.getVehicle_class(), 
				account.getPlate1(), account.getYear1(), account.getMake1(), account.getModel1(), account.getState1(), account.getColor1(), 
				account.getPlate2(), account.getYear2(), account.getMake2(), account.getModel2(), account.getState2(), account.getColor2(), WebServiceConfig.ACTION_USER_INFO);
		//Log.d("TEST", "==================="+params.toString());
		AsyncHttpPost httpPost = new AsyncHttpPost(
				baseActivity, 
				new AsyncHttpResponseProcess(this, true){
					@Override
					public void processIfResponseSuccess(JSONObject json) {
						super.processIfResponseSuccess(json);
						
						progressSuccess(json);
					}
					
				}, 
				params);
		httpPost.execute(WebServiceConfig.API_SUBMIT_USER_INFO);
	}
	
	//--------------xu ly ket qua tra ve------------------//
	public void progressSuccess(JSONObject json){
		try {
				
			account.set_tokenID(json.getString(WebServiceConfig.PARAM_TOKEN_ID));
			startActivity(new Intent(baseActivity, TripInfoActivity.class));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
	}

	// ------------------checkValidate-------------------//
	public boolean checkValidate() {
		if (StringUtility.isEmpty(this.et_year_1)
				|| StringUtility.isEmpty(this.et_plate_1)) {
			showPositiveCustomDialog(baseActivity, R.string.error,
					R.string.input_from_error, R.drawable.button_ok);
			findViewById(R.id.lnPlace1).setBackgroundResource(
					R.drawable.bg_edittext_error);
			findViewById(R.id.lnYear1).setBackgroundResource(
					R.drawable.bg_edittext_error);
			// this.et_year_1.setBackgroundResource(R.drawable.bg_edittext_error);
			// this.et_plate_1.setBackgroundResource(R.drawable.bg_edittext_error);
			return false;
		}
		
		Time localTime = new Time(Time.getCurrentTimezone());
		localTime.setToNow();
		
		if( (Integer.parseInt(et_year_1.getText().toString())<1851) || (Integer.parseInt(et_year_1.getText().toString())>1+localTime.year)){
			showPositiveCustomDialog(baseActivity, R.string.error,
					R.string.year_error, R.drawable.button_ok);
			this.et_year_1.requestFocus();
			findViewById(R.id.lnYear1).setBackgroundResource(
					R.drawable.bg_edittext_error);
			return false;
		}
		
		if (index == 1 || index == 2) {
			if (StringUtility.isEmpty(this.et_year_2)
					|| StringUtility.isEmpty(this.et_plate_2)) {
				showPositiveCustomDialog(baseActivity, R.string.error,
						R.string.input_from_error, R.drawable.button_ok);
				findViewById(R.id.lnPlace2).setBackgroundResource(
						R.drawable.bg_edittext_error);
				findViewById(R.id.lnYear2).setBackgroundResource(
						R.drawable.bg_edittext_error);
				// this.et_year_2.setBackgroundResource(R.drawable.bg_edittext_error);
				// this.et_plate_2.setBackgroundResource(R.drawable.bg_edittext_error);
				return false;
			}
			
			
			if( (Integer.parseInt(et_year_2.getText().toString())<1851) || (Integer.parseInt(et_year_2.getText().toString())>1+localTime.year)){
				showPositiveCustomDialog(baseActivity, R.string.error,
						R.string.year_error, R.drawable.button_ok);
				
				this.et_year_2.requestFocus();
				findViewById(R.id.lnYear2).setBackgroundResource(
						R.drawable.bg_edittext_error);
				
				return false;
			}
			
		}
		return true;
	}
}
